#include "Loader.h"

#include <QLibrary>
#include <QProcess>
#include <QDebug>
#include <QDir>


Loader::Loader(int argc, char *argv[], QObject *parent) : QObject(parent)
{
    //m_argc = argc;
    //m_argv = argv;
}

Loader::~Loader()
{
    mapLib.clear();
}

bool Loader::RunExecutableProgram(const QString& strProgram, const QStringList& strlistArguments)
{
    bool bRet = 0;
        /*记录Log
        QFile fileLog(QString("Update_%1.log").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd")));
        fileLog.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream tsLog(&fileLog);
        tsLog << QDateTime::currentDateTime().toString() << "\r\n";
        tsLog << "升级成功!" << "\r\n";
        tsLog << QString::number(m_Updater.oldVersion()) << "==>" << QString::number(m_Updater.newVersion()) << "\r\n";
        tsLog << "---------------------------------" << "\r\n\r\n";
        fileLog.flush();
        fileLog.close();


        QString strLog;
        strLog = _T("{\"success\":true,\"date\":\"%1\",\"version\":%2}")
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd"))
                .arg(m_Updater.newVersion());
        QByteArray baLog;
        baLog.append(strLog);
        HTTPRequest request;
        request.post(QUrl("http://mm.suwendjk.com/index.php/Index/Index/updateLog"), baLog);
        */



    //m_pMessageLabel->adjustSize();


    bRet = QProcess::startDetached(strProgram, strlistArguments);
    qDebug() << "[Loader RunExecutableProgram]startDetached:" << strProgram << strlistArguments
             << bRet;

    //qApp->exit();

    //QProcess myProcess;
    //QString command = "English.exe";
    //QStringList args;
    //args.append("-s");
    //args.append("leamus");
    //myProcess.start(m_strProgram, m_strlistArguments);
    //this->thread()->sleep(1);
    //myProcess.execute(command,args);//command是要执行的命令,args是参数
    //myProcess.waitForFinished();
    //qDebug()<<QString::fromLocal8Bit(myProcess.readAllStandardError());//获取输出
    //m_HttpDownload.

    return bRet;
}


int Loader::RunDynamicLib(const QString& libName, const QString& libFn, const QString& strArguments)
{
    int nRet = 0;

    QLibrary *lib = mapLib.value(libName, nullptr);
    if(nullptr == lib)  //如果没有载入过这个动态库
    {
        /*if(!QLibrary::isLibrary(libName))
        {
            qWarning() << "[!Loader]" << libName << "不是库或不存在";
            return -1;
        }*/

        lib = new QLibrary(libName, this);
        //QLibrary lib("libEnglishKill.1.0.0.dylib");
        //lib->setFileName(libName);
        //lib->setFileNameAndVersion(libName, 1);
        if(!lib->load())
        {
            qWarning() << "[!Loader RunDynamicLib]load library FAIL:" << lib->fileName() << "载入失败：" << lib->errorString();
            qWarning() << qApp->libraryPaths();
            delete lib;
            return -1;
        }

        mapLib[libName] = lib;
        qDebug() << "[Loader RunDynamicLib]" << libName << "载入结果：" << lib->isLoaded() << lib->fileName();
    }


    _libMain_ libMain = (_libMain_)lib->resolve(libFn.toLatin1().constData());

    if(nullptr == libMain)
    {
        qWarning() << "[!Loader RunDynamicLib]resolve FAIL:" << libFn << libMain << lib->errorString();
        return -2;
    }
    else
    {
        int nRet1 = libMain(strArguments.toLatin1().data());
        qDebug() << "[Loader RunDynamicLib]" << libFn << nRet1;
    }

    return nRet;
}
